package com.huawei.xme.ch02.t11;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄后林
 * @date 2022-10-18 10:54:47
 * @note 且行且珍惜
 */
public class T11Opt {

    // 15  11:10 写代码
    public static void main(String[] args) {
        T11Opt t = new T11Opt();
        List<String> list = t.encode(1, 49);
        for(String str: list){
            System.out.println(str);
        }
    }

    
    private List<String> getMiddleList(int begin, int end){
        
        //2、先定义一个结合把这些数据放进去  遍历  1 2 3 4 5 ... 49
        List<Integer> numberList = new ArrayList<>();
        for(int index = begin;index<=end;index++){
            numberList.add(index);
        }
        System.out.println(numberList);

        //3、 准备1个集合 把+装起来  +++++++ 48
        int counter =  end - begin;
        List<String> addList = new ArrayList<>();
        for(int index = 1;index<=counter;index++){
            addList.add("+");
        }
        //4、把数和运算符组合起来   1*2+3+4*5+6  ?
        List<String> middleList = new ArrayList<>();
        int left  = 0;
        int right = 0;
        int numberLength = numberList.size();
        int addLength=  addList.size();
        while (true){
            //1 左边拿1个
            if(left<= numberLength -1){
                int num  = numberList.get(left);
                middleList.add(""+num);
                left = left+1;
            }
            //2 右边拿1个
            if( right <= addLength -1){
                String add = addList.get(right);
                middleList.add(add);
                right = right +1;
            }
            //3 拿完了
            if ( left == numberList.size()){
                break;
            }
        }
        return middleList;
    }

    /**
     * 方法描述: 用于加密
     * @author: 黄后林
     * @date: 2022/10/18 11:23 
     * @param begin 开始
     * @param end   结束
     * @return java.util.List<java.lang.String>  结果集
     */
    public List<String> encode(int begin, int end){
        List<String>  rs = new ArrayList<>();

        // 1、入参检测
        if(begin<=0 || end<=0 || end <begin){
            return rs;
        }

        // 2、初始化数据
        List<String> middleList = getMiddleList(begin, end);

        for(int outer =0 ;outer<=middleList.size() -1; outer++){
            // 4.1、先找第1个+ 并且把它变成*  然后再找和第1个+匹配的第二个*  14  1+2+3+4+4
            String firstData = middleList.get(outer);
            if("+".equals(firstData)){
                // 说明找到了第1个+   70%
                String first = middleList.get(outer -1);

                // -----继续循环
                for(int inner = outer+1; inner<=middleList.size() -1;inner++){
                    String secondData = middleList.get(inner);
                    if("+".equals(secondData)){
                        if((inner -  outer) == 2){
                            // 说明2个加号是相邻的
                            continue;
                        }
                        // 说明找到了第2个+   70%
                        String second = middleList.get(inner -1);
                        rs.add(first + second);
                    }
                }
            }
        }
        return rs;
    }
}
